package com.it.jzs.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.jzs.dao.admin.AdminTagMapper;
import com.it.jzs.dao.admin.AdminTypeMapper;
import com.it.jzs.pojo.Tag;
import com.it.jzs.pojo.Type;
import com.it.jzs.service.admin.AdminTagService;
import com.it.jzs.service.admin.AdminTypeService;
import com.it.jzs.vo.AddBlogTagVo;
import com.it.jzs.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminTagServiceImpl implements AdminTagService {
    @Resource
    private AdminTagMapper adminTagMapper;

    @Override
    public Result<?> selectAll() {
        List<Tag> tagList = adminTagMapper.selectList(null);
        return Result.success(tagList, "测试");
    }

    @Override
    @Transactional
    public Result<Object> insertOne(Tag tag) {   // 插入一个分类
        String name = tag.getName();
        if (name.length() > 1 && name.length() <= 10) {  // 后端长度校验
            tag.setCreateTime(System.currentTimeMillis());
            tag.setUpdateTime(System.currentTimeMillis());
            int insert = adminTagMapper.insert(tag);
            if (insert != 0) {
                return Result.success(null, "新增成功");
            }
        }
        return Result.error(null, "新增失败");

    }

    @Override
    @Transactional
    public Result<Object> delete(Integer id) {
        if (id != null) {
//                Type type = typeMapper.selectById(id);
            int i = adminTagMapper.deleteById(id);
            if (i > 0) {
                return Result.success(null, "删除成功！");
            }
        }
        return Result.error(null, "删除失败！");
    }

    @Override
    @Transactional
    public Result<Object> update(Tag tag) {  // 修改分类
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        tag.setUpdateTime(System.currentTimeMillis());
//        wrapper.select("name").select("type_img").select("update_time");
//        int update = typeMapper.update(type, wrapper);
        if (tag.getName().length() < 1 || tag.getName().length() > 10) {
            return Result.error(null, "分类名长度应该2到10之间");
        }
        int update = adminTagMapper.updateById(tag);
        if (update > 0) {
            return Result.success(null, "修改成功");
        }
        return Result.error(null, "修改失败");
//        int i = typeMapper.updateById(type);


    }

    @Override
    public Result<Object> selectPage(Integer index, Integer size, String keyWords) { // 分页查询
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        if (!keyWords.equals("")) {
            wrapper.like("name", keyWords);
        }
        Page<Tag> page = new Page<>(index, size);

        Page<Tag> typePage = adminTagMapper.selectPage(page, wrapper);
        return Result.success(typePage, "获取分类分页数据成功");

    }

    @Override
    public Result<Object> findAll() {
        List<Tag> tagList = adminTagMapper.selectList(null);
        return Result.success(tagList, "获取所有标签数据");

    }

    @Override
    public Result<Object> findAddBlogTagVo() {
        List<Tag> tagList = adminTagMapper.selectList(null);
        ArrayList<AddBlogTagVo> list = new ArrayList<>();
        for (Tag tag : tagList) {
            AddBlogTagVo addBlogTagVo = new AddBlogTagVo();
            addBlogTagVo.setTagId(tag.getId());
            addBlogTagVo.setTagName(tag.getName());
            list.add(addBlogTagVo);
        }
        return Result.success(list, "获取添加博客分类数据成功");
    }

    @Override
    public Result<Object> deleteBatch(List<Integer> ids) {
        if (ids.size() > 0) {
            int i = adminTagMapper.deleteBatchIds(ids);
            if (i > 0) {
                return Result.success(null, "批量删除成功");
            }
        }

        return Result.error(null, "删除失败");

    }

//    @Override
//    public void insertOne() {
//        typeMapper.insert()
//    }
}
